package com.cat.binaryTree02;

import com.cat.binaryTree.ListNode;

import java.util.HashSet;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/linked-list-cycle-ii/
 * @create 2025/10/21 19:13
 * @since JDK17
 */

public class Solution03 {
    public ListNode detectCycle(ListNode head) {
        HashSet<ListNode> set = new HashSet<>();
        ListNode p = head;
        while (p != null) {
            if (set.contains(p)) {
                return p;
            }
            set.add(p);
            p = p.next;
        }

        return null;
    }
}
